﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shove.Database;


public partial class shop_Good_Manager : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            Utility.InitUltraWebGrid.InitDisplayLayOut(this.UWGGood);

            if (_Site != null)
            {
                hSiteID.Text = _Customer.ShopID.ToString();
            }
            ReadAction();            
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //RequestCompetences = Competences.BuildCompetencesList(Competences.shop_Good_Manager_Info);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void UWGGood_UpdateCell(object sender, Infragistics.WebUI.UltraWebGrid.CellEventArgs e)
    {
        long ShopID = Shove._Convert.StrToLong(hSiteID.Text, 0);
        //object shopStockID = e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("shopStockID"));            //查询仓库表中是否有该商品的依据
        //string goodID = e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("GoodID")).ToString();           //修改仓库表中某字段的依据           
        string goodID = e.Cell.Row.Cells[16].ToString(); 
        string shopStockID = e.Cell.Row.Cells[15].ToString();

        DAL.Tables.tbl_Shop_ShopStock tbl_ShopStock = new DAL.Tables.tbl_Shop_ShopStock();
       
        if (shopStockID != "")                                                   //仓库表中有该商品，修改其对应的属性
        {            
            long ID = Shove._Convert.StrToLong(shopStockID.ToString(), 0);

            if (e.Cell.Column.Key.ToString().Equals("isOnSale"))
            {                
                tbl_ShopStock.isOnSale.Value = Shove._Convert.StrToBool(e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("isOnSale")).ToString(), false);                
            }
            else if (e.Cell.Column.Key.ToString().Equals("isNew"))
            {
                tbl_ShopStock.isNew.Value = Shove._Convert.StrToBool(e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("isNew")).ToString(), false);                
            }
            else if (e.Cell.Column.Key.ToString().Equals("isHot"))
            {
                tbl_ShopStock.isHot.Value = Shove._Convert.StrToBool(e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("isHot")).ToString(), false);                
            }
            else if (e.Cell.Column.Key.ToString().Equals("IsCommend"))
            {
                tbl_ShopStock.IsCommend.Value = Shove._Convert.StrToBool(e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("IsCommend")).ToString(), false);                
            }
            else if (e.Cell.Column.Key.ToString().Equals("isFavorable"))
            {
                tbl_ShopStock.IsFavorable.Value = Shove._Convert.StrToBool(e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("isFavorable")).ToString(), false);                
            }

            if (tbl_ShopStock.Update("ID=" + ID) >= 0)
            {
                BindData();
            }
        }
        else                                                                       //仓库表中没有该商品时 要让其上架，就往仓库表里面添加一条记录                                                                   
        {
            if (e.Cell.Column.Key.ToString().Equals("isOnSale"))
            {
                tbl_ShopStock.Amount.Value = 0;
                tbl_ShopStock.GoodID.Value = Shove._Convert.StrToInt(e.Cell.Row.GetCellValue(this.UWGGood.Columns.FromKey("GoodID")).ToString(), 0);     
                tbl_ShopStock.ShopID.Value = ShopID;
                tbl_ShopStock.upDate.Value = System.DateTime.Now.ToString();
                tbl_ShopStock.isOnSale.Value = true;
                tbl_ShopStock.isNew.Value = false;
                tbl_ShopStock.isHot.Value = false;
                tbl_ShopStock.IsCommend.Value = false;
                tbl_ShopStock.IsFavorable.Value = false;

                if (tbl_ShopStock.Insert() > 0)
                {
                    BindData();
                }
            }
        }
    }

    protected void UWGGood_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        BindData();
    }

    protected void btnPicture_Click(object sender, EventArgs e)
    {

    }

    protected void btnDeteil_Click(object sender, EventArgs e)
    {
        string ID = hGoodID.Value;

        string GoodTypeID = new DAL.Tables.tbl_Shop_Good().Open("", "ID=" + ID, "").Rows[0]["GoodTypeID"].ToString();

        if (!ID.Equals(""))
        {
            Response.Redirect("~/page/Good_Edit2.aspx?Action=Edit&GoodID=" + ID + "&GoodTypeID=" + GoodTypeID);
        }
    }

    private void BindData()
    {
        long ShopID = Shove._Convert.StrToLong(hSiteID.Text, 0);
        DataTable dt = new DAL.Views.V_Site_GoodShopStock().Open("", "ShopID=" + ShopID + "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读取错误", "Good_Manager");
        }

        this.UWGGood.DataSource = dt;
        this.UWGGood.DataBind();
    }

    private void ReadAction()
    {
        string sAction = Shove._Web.Utility.GetRequest("Type");

        if (sAction != "Info")
        {
            btnDeteil.Visible = false;
        }

        if (_Site.ID != 1)
        {            
            UWGGood.Columns[7].Hidden = true;
            UWGGood.Columns[8].Hidden = true;
            UWGGood.Columns[9].Hidden = true;
            UWGGood.Columns[10].Hidden = true;
        }
    }
}
